<?php
    $reg = Registry::getInstance();
?>
<h2>Список</h2>

<?php print anchor('admin/table/add/'.$table, 'Добавить'); ?>

<br /><br />

<?php print form_open_multipart('admin/table/groupupdate/'.$table); ?>
    <table width="100%" border="1">
        <tr>
        <?php
            foreach($fields_config as $index => $item) {
            if(!isset($item['list']) || $item['list'] != TRUE) continue;
        ?>
            <th><?php print $fields_config[$index]['caption']; ?></th>
        <?php } ?>
        <th>Операции</th>
        </tr>
        <?php
            foreach($fields as $field) {
                print '<tr>';
                foreach($fields_config as $index => $item) {
                    $data['value'] = $field[$index];
                    $data['name'] = $index;
                    if(!isset($item['list']) || $item['list'] != TRUE) continue;
                    print '<td width="'.(isset($item['width']) ? $item['width'] : '0%').'">';
                    switch($item['type']) {

                        case 'text':                            
                            if(isset($item['rtable']) && isset($item['rfield'])) {
                                $res = $this->db->select($item['rfield'])->where('id', $field[$index])->limit(1)->get($item['rtable']);
                                $sub = $res->row_array(); 
                                print form_label($sub[$item['rfield']], $index);                                                               
                            } else {
                                $data['name'] = $index.'['.$field['id'].']';
                                $data['size'] = 20;
                                print form_input($data);
                            }                            
                        break;

                        case 'number':
                            $data['name'] = $index.'['.$field['id'].']';
                            $data['size'] = 10;
                            print form_input($data);
                        break;

                        case 'select_dir':
                            if(!isset($reg[$item['rtable'].'_sql_'.$field[$index]])) {
                                $res = $this->db->select($item['rfield'])->where('id', $field[$index])->get($item['rtable']);
                                $reg[$item['rtable'].'_sql_'.$field[$index]] = $res;
                            } else {
                                $res = $reg[$item['rtable'].'_sql_'.$field[$index]];
                            }

                            $sub = $res->row_array();

                            if(count($sub) > 0) {
                                /*foreach($sub as $s) {
                                    $dropdown[$s['id']] = $s[$item['rfield']];
                                }
                                print form_dropdown($index.'['.$index.']['.$field['id'].']', $dropdown, $field[$index]);*/
                                print form_label($sub[$item['rfield']], $index);
                            } else {
                                print form_label('Нет', $index);
                            }
                            $sub = array();
                            $res->free_result();
                        break;                                                

                        case 'checkbox':
                            //print form_checkbox($index.'['.$field['id'].']', 1, $field[$index] == 1 ? TRUE : FALSE);
                            //@todo - group update checkboxes
                            print form_label($field[$index] == 1 ? 'Да' : 'Нет', $index);
                        break;

                        default:
                            print form_label($data['value'], $index);
                    }
                    print '</td>';
                }
                print '
                <td>
                    '.anchor('admin/table/edit/'.$table.'/'.$field['id'], '[edit]').'
                    '.anchor('admin/table/delete/'.$table.'/'.$field['id'], '[delete]').'
                </td>';
                print '</tr>';
            }
        ?>
    </table>
    <br />
    <input type="submit" name="<?php print $table.'_'.$action.'_submit'; ?>" value="Сохранить">
<?php print form_close(); ?>